package com.jacen.fzxs.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.jacen.fzxs.dao.IImageDao;
import com.jacen.fzxs.entity.ImageEntity;
import com.jacen.fzxs.util.SqlCondition;
import com.jacen.fzxs.util.Utils;


/**
 * 图片dao
 * @author Jacen
 * 2014年11月18日 下午9:20:23
 */
@Repository
public class ImageDao extends BaseDao<ImageEntity> implements IImageDao {

	@Override
	public  List<Map<String, Object>> getPage(int beginRow, int endRow,
			Map<String, ? extends Object> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "SELECT i.*,w.* from t_image i left join t_wall w on i.wid = w.id  where 1=1 %s ";
		sql = String.format(sql, cond.getCondition());
		sql = SqlCondition.pageSql(sql, beginRow, endRow);
		return getJdbcTemplate().queryForList(sql,cond.getParams().toArray());
	}

	@Override
	public List<ImageEntity> getList(String wid) {
		String sql = "SELECT * from t_image where wid =  '%s'";
		sql = String.format(sql, wid);
		return executeForList(sql);
	}
	
	@Override
	public long getCount(Map<String, ? extends Object> paramMap)
			throws SQLException {
		SqlCondition cond = getPageCondition(paramMap);
		String sql = "select count(1) from t_image where 1=1 %s";
		sql = String.format(sql, cond.getCondition());
		return executeForLong(sql, cond.getParams().toArray());
	}
	
	
	
	private SqlCondition getPageCondition(
			Map<String, ? extends Object> mapParams) {
		SqlCondition cond = new SqlCondition();
		if (Utils.paramIsNotEmpty(mapParams.get("cname")))
			cond.and().like("cname", mapParams.get("cname"));
		return cond;
	}

	


}
